package ee.maoinvest.frontend.controller.requestprocessors;

import java.sql.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import ee.maoinvest.backend.model.data.ElectricityContract;
import ee.maoinvest.backend.model.data.ElectricityMeter;
import ee.maoinvest.backend.model.data.Enterprise;
import ee.maoinvest.backend.model.data.views.EnterpriseContractWithElectricityMeter;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriserelationswithelectricitymetersandcontractsforms.ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriserelationswithelectricitymetersandcontractsforms.EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriserelationswithelectricitymetersandcontractsforms.InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter;

public class EnterpriseRelationsWithElectricityMeterAndContractFormsRequestProcessor 
{
	private final String EMPTY_STRING = "";
	private final int GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR = -1;
	
	public EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter 
				fillenterpriseRelationsWithElectricityMetersAndItsContractsFormConverterWithRequestData(HttpServletRequest request)
	{
		EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter = 
				new EnterpriseRelationsWithElectricityMetersAndItsContractsFormConverter();
		
		String enterpriseID = request.getParameter("enterpriseID");
		String enterpriseContractWithElectricityMeterIDToDelete = request.getParameter("enterpriseContractWithElectricityMeterIDToDelete");
		String daytimeReadingWhileEndingAContract = request.getParameter("daytimeReadingWhileEndingAContract");
		String nightTimeReadingWhileEndingAContract = request.getParameter("nightTimeReadingWhileEndingAContract");
		String reactivePowerReadingWhileEndingAContract = request.getParameter("reactivePowerReadingWhileEndingAContract");
		String contractPeriodEndDate = request.getParameter("contractPeriodEndDate");
		
		HttpSession session = request.getSession();
		Enterprise enterpriseWhichRelationsAreBeingModified = (Enterprise) session.getAttribute("enterpriseWhichRelationsAreBeingModified");
		@SuppressWarnings("unchecked")
		List<EnterpriseContractWithElectricityMeter> listOfAllEnterpriseContractsWithElectricityMeters = 
				(List<EnterpriseContractWithElectricityMeter>) session.getAttribute("listOfAllEnterpriseContractsWithElectricityMeters");
		Date latestDateAfterWhatDataManipulationIsPossible = (Date) session.getAttribute("latestDateAfterWhatDataManipulationIsPossible");
		
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setLatestDateAfterWhatDataManipulationIsPossible(latestDateAfterWhatDataManipulationIsPossible);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setEnterpriseID(enterpriseID);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setEnterpriseContractWithElectricityMeterIDToDelete(enterpriseContractWithElectricityMeterIDToDelete);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setDaytimeReadingWhileEndingAContract(daytimeReadingWhileEndingAContract);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setNightTimeReadingWhileEndingAContract(nightTimeReadingWhileEndingAContract);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setReactivePowerReadingWhileEndingAContract(reactivePowerReadingWhileEndingAContract);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setContractPeriodEndDate(contractPeriodEndDate);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setEnterpriseWhichRelationsAreBeingModified(enterpriseWhichRelationsAreBeingModified);
		enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter.setListOfAllEnterpriseContractsWithElectricityMeters(listOfAllEnterpriseContractsWithElectricityMeters);
		
		return enterpriseRelationsWithElectricityMetersAndItsContractsFormConverter;
	}
	
	public InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter fillInsertNewContractBetweenEnterpriseAndElectricityMeterFormConverterWithRequestData(
			HttpServletRequest request)
	{
		InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter = 
				new InsertNewContractBetweenEnterpriseAndElectricityMeterFormConverter();
		
		String contractPeriodStartDate = request.getParameter("contractPeriodStartDate");
		String electricityMeterSelectedID = request.getParameter("electricityMeterSelectedID");
		String electricityContractSelectedID = request.getParameter("electricityContractSelectedID");
		String electricityMeterDaytimeReadingAtRelationStartDate = request.getParameter("electricityMeterDaytimeReadingAtRelationStartDate");
		String electricityMeterNightTimeReadingAtRelationStartDate = request.getParameter("electricityMeterNightTimeReadingAtRelationStartDate");
		String electricityMeterReactivePowerReadingAtRelationStartDate = request.getParameter("electricityMeterReactivePowerReadingAtRelationStartDate");
		
		HttpSession session = request.getSession();
		Enterprise enterpriseWhichRelationsAreBeingModified = (Enterprise) session.getAttribute("enterpriseWhichRelationsAreBeingModified");
		@SuppressWarnings("unchecked")
		List<ElectricityMeter> listOfAllElectricityMetersValidToMakeContractWith = (List<ElectricityMeter>) session.getAttribute("listOfAllElectricityMetersValidToMakeContractWith");
		@SuppressWarnings("unchecked")
		List<ElectricityContract> listOfAllElectricityContracts = (List<ElectricityContract>) session.getAttribute("listOfAllElectricityContracts");
	
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setContractPeriodStartDate(contractPeriodStartDate);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setElectricityMeterSelectedID(electricityMeterSelectedID);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setElectricityContractSelectedID(electricityContractSelectedID);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setElectricityMeterDaytimeReadingAtRelationStartDate(electricityMeterDaytimeReadingAtRelationStartDate);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setElectricityMeterNightTimeReadingAtRelationStartDate(electricityMeterNightTimeReadingAtRelationStartDate);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setElectricityMeterReactivePowerReadingAtRelationStartDate(electricityMeterReactivePowerReadingAtRelationStartDate);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setEnterpriseWhichRelationsAreBeingModified(enterpriseWhichRelationsAreBeingModified);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setListOfAllElectricityMetersValidToMakeContractWith(listOfAllElectricityMetersValidToMakeContractWith);
		insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter.setListOfAllElectricityContracts(listOfAllElectricityContracts);
		
		return insertNewContractBetweenEnterpriseAndElectricityMeterFormConverter;
	}
	
	public ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter fillElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverterWithRequestData(
			HttpServletRequest request)
	{
		ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter = 
				new ElectricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter();
		
		String electricityContractID = request.getParameter("electricityContractID");
		String contractRelationWithElectricityMeterAndEnterpriseIDToDelete = request.getParameter("contractRelationWithElectricityMeterAndEnterpriseIDToDelete");
		String daytimeReadingWhileEndingAContract = request.getParameter("daytimeReadingWhileEndingAContract");
		String nightTimeReadingWhileEndingAContract = request.getParameter("nightTimeReadingWhileEndingAContract");
		String reactivePowerReadingWhileEndingAContract = request.getParameter("reactivePowerReadingWhileEndingAContract");
		String contractPeriodEndDate = request.getParameter("contractPeriodEndDate");
		
		HttpSession session = request.getSession();
		
		ElectricityContract electricityContractWhichRelationsAreBeingModified = (ElectricityContract) session.getAttribute("electricityContractWhichRelationsAreBeingModified");
		@SuppressWarnings("unchecked")
		List<EnterpriseContractWithElectricityMeter> listOfAllContractRelationsWithElectricityMetersAndEnterprises = 
				(List<EnterpriseContractWithElectricityMeter>) session.getAttribute("listOfAllContractRelationsWithElectricityMetersAndEnterprises");
		Date latestDateAfterWhatDataManipulationIsPossible = (Date) session.getAttribute("latestDateAfterWhatDataManipulationIsPossible");
		
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setLatestDateAfterWhatDataManipulationIsPossible(latestDateAfterWhatDataManipulationIsPossible);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setElectricityContractID(electricityContractID);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setContractRelationWithElectricityMeterAndEnterpriseIDToDelete(contractRelationWithElectricityMeterAndEnterpriseIDToDelete);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setDaytimeReadingWhileEndingAContract(daytimeReadingWhileEndingAContract);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setNightTimeReadingWhileEndingAContract(nightTimeReadingWhileEndingAContract);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setReactivePowerReadingWhileEndingAContract(reactivePowerReadingWhileEndingAContract);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setContractPeriodEndDate(contractPeriodEndDate);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setElectricityContractWhichRelationsAreBeingModified(electricityContractWhichRelationsAreBeingModified);
		electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter.setListOfAllContractRelationsWithElectricityMetersAndEnterprises(listOfAllContractRelationsWithElectricityMetersAndEnterprises);
		
		return electricityContractRelationsWithElectricityMetersAndEnterprisesFormConverter;
	}
	
	public int getEnterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
		
		String enterpriseID = request.getParameter("enterpriseIDWhichRelationsWithElectricityMetersAndContractsToEdit");
		if (enterpriseID != null && !enterpriseID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(enterpriseID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
	
	public int getEnterpriseIDWithWhoNewContractWithElectricityMeterIsToBeInserted(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
		
		String enterpriseID = request.getParameter("enterpriseIDWithWhoNewContractIsToBeMade");
		if (enterpriseID != null && !enterpriseID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(enterpriseID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
	public int getContractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
		
		String enterpriseID = request.getParameter("contractIDWhichRelationsWithElectricityMetersAndEnterprisesToEdit");
		if (enterpriseID != null && !enterpriseID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(enterpriseID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
}
